package net.shopxx.entity;

import java.math.BigDecimal;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import net.shopxx.util.SystemConfigUtil;

/**
 * 实体类 - 发货
 */

@Entity
@Table(name = "shippingFee")
public class ShippingFee extends BaseEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7188551023697451951L;

	// express
	public BigDecimal express_postage;
	
	public Integer express_num;
	
	public BigDecimal express_plus;

	// ems
	public BigDecimal ems_postage;
	
	public Integer ems_num;
	
	public BigDecimal ems_plus;

	// post
	public BigDecimal post_postage;
	
	public Integer post_num;
	
	public BigDecimal post_plus;
	
	public Set<Product> productSet;
	
	@Column(nullable = false)
	public BigDecimal getExpress_postage() {
		return express_postage;
	}

	public void setExpress_postage(BigDecimal express_postage) {
		this.express_postage = express_postage;
	}

	@Column(nullable = false)
	public BigDecimal getExpress_plus() {
		return express_plus;
	}

	public void setExpress_plus(BigDecimal express_plus) {
		this.express_plus = express_plus;
	}

	@Column(nullable = false)
	public BigDecimal getEms_postage() {
		return ems_postage;
	}

	public void setEms_postage(BigDecimal ems_postage) {
		this.ems_postage = ems_postage;
	}

	@Column(nullable = false)
	public BigDecimal getEms_plus() {
		return ems_plus;
	}

	public void setEms_plus(BigDecimal ems_plus) {
		this.ems_plus = ems_plus;
	}

	@Column(nullable = false)
	public BigDecimal getPost_postage() {
		return post_postage;
	}

	public void setPost_postage(BigDecimal post_postage) {
		this.post_postage = post_postage;
	}

	@Column(nullable = false)
	public BigDecimal getPost_plus() {
		return post_plus;
	}
	
	public void setPost_plus(BigDecimal post_plus) {
		this.post_plus = post_plus;
	}

	public Integer getExpress_num() {
		return express_num;
	}

	public void setExpress_num(Integer express_num) {
		this.express_num = express_num;
	}

	public Integer getEms_num() {
		return ems_num;
	}

	public void setEms_num(Integer ems_num) {
		this.ems_num = ems_num;
	}

	public Integer getPost_num() {
		return post_num;
	}

	public void setPost_num(Integer post_num) {
		this.post_num = post_num;
	}
	
	
	@OneToMany(mappedBy = "shippingFee", fetch = FetchType.LAZY)
	@Cascade(value = { CascadeType.REMOVE })
	public Set<Product> getProductSet() {
		return productSet;
	}

	public void setProductSet(Set<Product> productSet) {
		this.productSet = productSet;
	}

	@Transient
	public BigDecimal getDeliveryFree(int type,int num)
	{
		BigDecimal ret = new BigDecimal("0");
		int base = 1;
		double plus_num = 0;
		BigDecimal baseFree = null;
		BigDecimal plusFree = null;
		switch(type)
		{
		case 0:
			plus_num = Math.ceil((num-base)/Double.parseDouble(String.valueOf(post_num)));
			baseFree = this.getPost_postage();
			plusFree = this.getPost_plus();
			break;
		case 1:
			plus_num = Math.ceil((num-base)/Double.parseDouble(String.valueOf(express_num)));
			baseFree = this.getExpress_postage();
			plusFree = this.getExpress_plus();
			break;
		case 2:
			plus_num = Math.ceil((num-base)/Double.parseDouble(String.valueOf(ems_num)));
			baseFree = this.getEms_postage();
			plusFree = this.getEms_plus();
			break;
		}
		ret = baseFree.add(plusFree.multiply(new BigDecimal(plus_num)));
		return ret;
	}
	
	

}